import WxValidate from '../../utils/WxValidate';
// pages/entryWant/entryWant.js  我要报单

//定义本页面
let app = getApp();  

let that;

// pages/entryQuick/entryQuick.js   快速报单
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "imageUploaded": {
      "id": 'imageUploaded'
    },
    "category": {
      "array": ["服务器", "路由器", "打印机", "交换机", "数据库服务器", "硬件", "软件"],
      "title": "类别",
      "id": "category",
      "index": ''
    },
    "description": {
      "title": "描述",
      "id": "description",
      "value": "",
    },
    "nextActivityId": {
      "title": "主办人",
      "id": "nextActivityId",
      "value": {                   //当前列表中选中的radio 的id 与值
        "idArray": "",
        "nameArray": ""
      }
    },
    "nextActPersonId": {
      "title": "经办人",
      "id": "nextActPersonId",
      "value": {                   //当前列表中选中的radio 的id 与值
        "idArray": "",
        "nameArray": ""
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    that = this;  //获取本页面

    that.setData({
      "imageUploaded": {
        "id": 'imageUploaded'
      },
      "category": {
        "array": ["服务器", "路由器", "打印机", "交换机", "数据库服务器", "硬件", "软件"],
        "title": "类别",
        "id": "category",
        "index": ''
      },
      "description": {
        "title": "描述",
        "id": "description",
        "value": "",
      },
      "nextActivityId": {
        "title": "主办人",
        "id": "nextActivityId",
        "value": {                   //当前列表中选中的radio 的id 与值
          "idArray": "",
          "nameArray": ""
        }
      },
      "nextActPersonId": {
        "title": "经办人",
        "id": "nextActPersonId",
        "value": {                   //当前列表中选中的radio 的id 与值
          "idArray": "",
          "nameArray": ""
        }
      },
    });
    
    //表单-验证字段 初始化
    that.initValidate();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },



  //表单提交
  formSubmit: function (re) {

    // console.log(re.detail.value);
    // 传入表单数据，调用验证方法

    console.log('form', re.detail.value, 'images', that.data.nextActPersonId)
    console.log(re.detail.value);
    const params = re.detail.value;
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      app.showModal(error)
      return false
    } else {
      console.log('form', re.detail.value, 'images', that.data.images)
    }
  },

  //表单-验证字段
  initValidate() {

    // 验证字段的规则
    const rules = {
      category: {   //类别
        required: true,
      },
      description: {  //描述
        required: true,
        rangelength: [5, 200]
      }

    }

    // 验证字段的提示信息，若不传则调用默认的信息
    const messages = {
      category: {
        required: '请选择类别',
      },
      description: {
        required: "请输入描述",
        rangelength: '请输入长度在 5 到 200 之间的字符。'
      }
    }

    // 创建实例对象
    that.WxValidate = new WxValidate(rules, messages);

  }

})